<?php
/**
 * Plugin Name: wp-cache-db
 * Short Name: db_cache
 * Description: 保存查询数据 
 * Author: midoks
 * Version: 1.0
 * Requires at least: 2.7
 * Tested up to: 3.4
 * Tags: db cache
 * Contributors: midoks
 * Author URI: http://midoks.cachecha.com/
 */

// --- 缓存设置 start ---
//插件地址
if (!defined('WP_PLUGIN_DIR')){
	define('WP_PLUGIN_DIR', WP_CONTENT_DIR.'/plugins');
}

//缓存插件地址
if ( !defined( 'CACHE_PATH' ) ) {
	define('CACHE_PATH', WP_PLUGIN_DIR.'/wp-cache-db/');
}
// --- 缓存设置 end ---

include_once('common.php');
//更新配置
if(isset($_POST['wpcachedb_options']) && $_POST['submit'] == '保存设置'){
	$option['method'] = $_POST['wpcachedb_options']['method']; //保存的方式
	$option['enabled'] = $_POST['wpcachedb_options']['enabled'];//是否支持插件启用
	if(is_null($option['enabled'])){
		$option['enabled'] = false;
	}
	$option['timeout'] = $_POST['wpcachedb_options']['timeout'];//保存的时间(分钟)
	update_wp_db_cache_options('wpcachedb_options', $option);
}

if('清空数据' == $_POST['submit']){
	wp_db_cache_clean();
}

if('清空过期数据' == $_POST['submit']){
	wp_db_cache_clean_expire();
}

//清空数据
function wp_db_cache_clean(){
	global $wpdb;
	$wpdb->wp_db_cache_clean();
}

//清空过期数据
function wp_db_cache_clean_expire(){
	global $wpdb;
	$wpdb->wp_db_cache_clean_expire();
}

class wpcachedb{

	//配置名字
	public $cfg_name = 'wpcachedb_options';

	//架构函数|插件初始化
	public function __construct(){
		
		//初始化插件
		add_action( 'init', array( &$this, 'init' ));

		//初始化后台选项
		//register_activation_hook(__FILE__, array(&$this, 'wpcachedb_install'));
		add_action( 'activate_'.plugin_basename( __FILE__ ), array(&$this, 'wpcachedb_install'));
		//删除配置信息
		//register_uninstall_hook(__FILE__, array(&$this, 'wpcachedb_uninstall'));
		//$this->wpcachedb_uninstall();
		//register_deactivation_hook(__FILE__, array(&$this, 'wpcachedb_uninstall'));
		add_action( 'deactivate_'.plugin_basename(__FILE__), array(&$this, 'wpcachedb_uninstall'), 10);

		//$url = plugin_dir_url(__FILE__);
		//后台选项面板初始化
		if(is_admin()){
			add_action('admin_init', array($this, 'wpcachedb_menu_init'));
			add_filter('plugin_action_links', array($this, 'wpcachedb_action_links'), 10, 2);
			add_action('admin_menu', array($this, 'wpcachedb_menu'));
		}
	}

	//插件初始化
	public function init(){}

	//插件卸载执行
	public function wpcachedb_uninstall(){
		//T(WP_CONTENT_DIR.'/db.php');
		@unlink(WP_CONTENT_DIR.'/db.php');
		delete_wp_db_cache_options($this->cfg_name);
	}


	//初始化后台选项
	public function wpcachedb_install(){
		$option['method'] = 'local'; 	//保存的方式
		$option['enabled'] = false;		//是否支持插件启用
		$option['timeout'] = 5;			//保存的时间(分钟)
		init_wp_db_cache_options($this->cfg_name, $option);
		@copy(CACHE_PATH.'db.php', WP_CONTENT_DIR.'/db.php');
	}

	//初始化后台选项设置
	public function wpcachedb_action_links($links, $file) {
    	if ( basename($file) != basename(plugin_basename(__FILE__))){
			return $links;
		}
    	$settings_link = '<a href="options-general.php?page=midoks_wpcachedb">设置</a>';
    	array_unshift($links, $settings_link);
    	return $links;
	}

	//点击选项页
	public function wpcachedb_menu(){
		add_options_page('DbCache',
						'数据库缓存设置',
						'manage_options',
						'midoks_wpcachedb',
						array($this, 'wpcachedb_menu_config'));
	}

	//面板设置
	public function wpcachedb_menu_config(){
		$options = get_wp_db_cache_options($this->cfg_name);
		echo '<div class="wrap">';
		echo '<h2>'.'DbCache Config(midoks)'.'</h2>';
		echo '<div class="narrow">';
		echo '<form  method="post">';
		echo '<p>数据缓存设置</p>';
			settings_fields('wpcachedb');
			do_settings_sections('wpcachedb');
		echo '<p class="submit"><input name="submit" type="submit" class="button-primary" value="保存设置" />';
		echo '<input style="margin-left:10px;" name="submit" type="submit" class="button-primary" value="清空数据" />
		<input style="margin-left:10px;" name="submit" type="submit" class="button-primary" value="清空过期数据" /></p>';
		echo '</form>';
		echo '</div>';
		echo '</div>';
	}

	//面板设置初始化
	public function wpcachedb_menu_init(){
		register_setting('wpcachedb_options', 'wpcachedb_options',  'wpcachedb_options_validate');
		add_settings_section('wpcachedb_main', __('设置','sh'), array($this, 'wpcachedb_section'), 'wpcachedb');
		
		//是否支持
		add_settings_field('enabled', __('是否支持','sh'), array($this, 'wpcachedb_enabled'), 'wpcachedb', 'wpcachedb_main');
		//保存的方式
		add_settings_field('method', __('保存的方式','sh'), array($this, 'wpcachedb_method'), 'wpcachedb', 'wpcachedb_main');
		//缓存时间
		add_settings_field('timeout', __('超时时间','sh'), array($this, 'wpcachedb_timeout'), 'wpcachedb', 'wpcachedb_main');
	}

	
	public function wpcachedb_section() {
		echo '请配置你的设置'.__('<p>Please enter your config.</p>','sh');
	}

	//保存的方式
	public function wpcachedb_method(){
		$options = get_wp_db_cache_options($this->cfg_name);?>
		<select name="wpcachedb_options[method]" id="method" />
			<option value="local" <?php if('local'==$options['method']) echo "selected='selected'"; ?>>local</option>
			<option value="mc" <?php if('mc'==$options['method']) echo "selected='selected'"; ?>>memcached</option>
			<option value="localMc" <?php if('localMc'==$options['method']) echo "selected='selected'"; ?>>memcached(本地版)</option>
			<option value="baeMc" <?php if('baeMc'==$options['method']) echo "selected='selected'"; ?>>memcached(百度云版)</option>
			<option value="redis" <?php if('redis'==$options['method']) echo "selected='selected'"; ?>>redis</option>
			<option value="baeRedis" <?php if('baeRedis'==$options['method']) echo "selected='selected'"; ?>>redis(百度云版)</option>
		</select><br /><?php
	}

	
	public function wpcachedb_enabled(){
		$options = get_wp_db_cache_options($this->cfg_name);?>
		<input type="checkbox" name="wpcachedb_options[enabled]" id="enabled" value="true" <?php
		if($options['enabled'] == 'true'){ echo 'checked="checked"';}?> /><br /><?php
	}

	public function wpcachedb_timeout(){
		$options = get_wp_db_cache_options($this->cfg_name);?>
		<input type="text" name="wpcachedb_options[timeout]" id="timeout" value="<?php echo $options['timeout']; ?>" />(minutes)<br /><?php
	}

	
	public function wpcachedb_options_validate($input){
		//var_dump($_GET);
		//var_dump($input);
    	//$input['enabled'] = is_int($input['enabled']) ? $input['enabled'] : 0;
    	//$input['timeout'] = is_int($input['timeout']) ? $input['timeout'] : 5;
    	return $input;
	}
}
//实例化
$w_p_d = new wpcachedb();
